package com.ruoyi.mes.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.mes.vo.plan.MtrPlanVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.domain.MtrPlan;
import com.ruoyi.mes.service.IMtrPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 物料计划Controller
 *
 * @author LongAndHao
 * @date 2023-04-04
 */
@Api(tags = "物料计划")
@RestController
@RequestMapping("/mes/mtrplan")
public class MtrPlanController extends BaseController {
    @Autowired
    private IMtrPlanService mtrPlanService;

    /**
     * 查询物料计划列表
     */
    @ApiOperation(value = "查询物料计划列表")
    @PreAuthorize("@ss.hasPermi('mes:mtrplan:list')")
    @GetMapping("/list")
    public TableDataInfo list(MtrPlan mtrPlan) {
        startPage();
        List<MtrPlan> list = mtrPlanService.selectMtrPlanList(mtrPlan);
        return getDataTable(list);
    }

    /**
     * 导出物料计划列表
     */
    @ApiOperation(value = "导出物料计划列表")
    @PreAuthorize("@ss.hasPermi('mes:mtrplan:export')")
    @Log(title = "物料计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MtrPlan mtrPlan) {
        List<MtrPlan> list = mtrPlanService.selectMtrPlanList(mtrPlan);
        ExcelUtil<MtrPlan> util = new ExcelUtil<MtrPlan>(MtrPlan.class);
        util.exportExcel(response, list, "物料计划数据");
    }

    /**
     * 获取物料计划详细信息
     */
    @ApiOperation(value = "获取物料计划详细信息")
    @PreAuthorize("@ss.hasPermi('mes:mtrplan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(mtrPlanService.selectMtrPlanById(id));
    }

    /**
     * 新增物料计划
     */
    @ApiOperation(value = "新增物料计划")
    @PreAuthorize("@ss.hasPermi('mes:mtrplan:add')")
    @Log(title = "物料计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MtrPlan mtrPlan) {
        return toAjax(mtrPlanService.insertMtrPlan(mtrPlan));
    }

    /**
     * 修改物料计划
     */
    @ApiOperation(value = "修改物料计划")
    @PreAuthorize("@ss.hasPermi('mes:mtrplan:edit')")
    @Log(title = "物料计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MtrPlan mtrPlan) {
        return toAjax(mtrPlanService.updateMtrPlan(mtrPlan));
    }

    /**
     * 删除物料计划
     */
    @ApiOperation(value = "删除物料计划")
    @PreAuthorize("@ss.hasPermi('mes:mtrplan:remove')")
    @Log(title = "物料计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mtrPlanService.deleteMtrPlanByIds(ids));
    }

    //******************************************************
    /**
     * 生成采购需求
     * @param id 物料计划id
     * @return
     */
    @PostMapping("/createBuyNeed")
    public AjaxResult createBuyNeed(@RequestParam Long id) {
        return mtrPlanService.createBuyNeed(id);
    }

    /**
     * 查 - 物料计划分页 - 物料计划编号
     * @param mtrPlan
     * @return
     */
    @GetMapping("/mtrPlanList")
    public TableDataInfo mtrPlanList(MtrPlan mtrPlan){
        startPage();
        List<MtrPlan> list = mtrPlanService.mtrPlanList(mtrPlan);
        return getDataTable(list);
    }

    /**
     * 查 - 物料计划详情 - 物料计划id
     * @param id
     * @return
     */
    @GetMapping("/getDetails")
    public AjaxResult getDetails(@RequestParam Long id){
        return success(mtrPlanService.getDetails(id));
    }

    /**
     * 撤回 - 物料计划及采购需求
     * @param id 物料计划id
     * @return
     */
    @DeleteMapping("/revokePlanAndNeed")
    public AjaxResult revokePlanAndNeed(@RequestParam Long id){
        return mtrPlanService.revokePlanAndNeed(id);
    }
}
